home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / include / fexprnew.h < prev    next >
C/C++ Source or Header  |  1993-08-18  |  5KB  |  209 lines

  1. /* $Id$ */
  2. /* new fast expression library! wheee! */
  3.  
  4. #ifndef fexpr_h_included
  5. #define fexpr_h_included
  6.  
  7. typedef enum {
  8.   FNONE, FREAL, FCOMPLEX, FDERIV, FRDERIV
  9. } ftype;
  10.  
  11. struct fexpression
  12. {
  13.   struct fcode *code;
  14.   struct ffunctable *funcs;
  15.   ftype type;
  16. };
  17.  
  18. typedef struct {
  19.   double real,imag;
  20. } fcomplex;
  21.  
  22. struct fcode;            /* forward reference */
  23. typedef struct fvalue_struct fvalue;
  24.  
  25. typedef struct {
  26.   int nderivs;
  27.   fvalue *values;
  28. } fjet;
  29.  
  30. struct fvalue_struct {
  31.   ftype type;
  32.   union fval_union {
  33.     fcomplex complex;
  34.     double real;
  35.     fjet deriv;
  36.   } value;
  37. };
  38.  
  39.  
  40. typedef struct {
  41.   struct {
  42.     double (*real)(int nargs, double *args, void *);
  43.     void *data;
  44.   } r;
  45.   struct {
  46.     void (*complex)(int nargs, fcomplex *args, fcomplex *op, void *);
  47.     void *data;
  48.   } c;
  49.   struct {
  50.     void (*deriv)(int nargs, fjet *args, fjet *op, void *data);
  51.     void *data;
  52.   } d;
  53. } fevalfunc;
  54.  
  55. struct ffuncstruct {
  56.   char *name;
  57.   fevalfunc function;
  58.   int nargs;
  59. };
  60.  
  61. #if 0
  62.  
  63. struct ffunctableent {
  64.   char *name;
  65.   fevalfunc function;
  66.   int nargs;            /* negative if we don't
  67.                  * care. otherwise, the only numbers
  68.                  * of arguments this will take. 0
  69.                  * means probably is just a constant
  70.                  * variable. */
  71. };
  72.  
  73. #endif
  74.  
  75. #if 0
  76. struct ffunctiontableent {
  77.   char *name;
  78.   ftype type;
  79.   void *function;
  80.   int nargs;
  81. };
  82. #endif
  83.  
  84. struct fcode {
  85.   int ninsts;
  86.   struct finst {
  87.     enum {
  88.       FIFUNC, FINUMBER
  89.     } type;
  90.     union {
  91.       struct {
  92.     int nargs;
  93.     struct ffuncstruct ent; /* functions are null if they haven't
  94.                    been linked. */
  95.       } func;
  96.       fvalue number;
  97.     } u;
  98.   } *insts;
  99. };
  100.  
  101. struct ffunctable {
  102.   int nents;
  103.   struct ffuncstruct *ents;
  104.   struct ffunctable *next;
  105. };
  106.  
  107. #if 0
  108. typedef struct ffunctiontableent ffunctiontable[];
  109. #endif
  110.  
  111.  
  112. /* functions */
  113.  
  114. /* operates on a whole bundle -- for simple users */
  115. extern struct fexpression *fnew_expr(ftype);
  116. extern char *fexpr_parse(struct fexpression *, char *);
  117. extern fvalue *fexpr_getvar(struct fexpression *, char *name);
  118. extern void fexpr_evaluate(struct fexpression *, fvalue *op);
  119. extern void fexpr_free(struct fexpression *);
  120. extern void fexpr_cleanup(void); /* run at end of program */
  121.  
  122. /* code functions */
  123. extern struct fcode *fnew_code(void);
  124. extern char *fcode_parse(struct fcode *, char *);
  125. extern int fcode_link(struct fcode *, struct ffunctable *, ftype);
  126. extern void fcode_run(struct fcode *, ftype, fvalue *op, ...);
  127. extern void fcode_free(struct fcode *);
  128.  
  129. extern void fapplyv(struct ffuncstruct *, ftype, fvalue *op, int nargs, ...);
  130. extern void fapply(struct ffuncstruct *, ftype, int nargs, fvalue *args, fvalue *op);
  131.  
  132. /* func functions */
  133. extern struct ffunctable *fnew_func(void);
  134. extern struct ffunctable *fnew_func_with_defaults(void);
  135. extern fvalue *ffunc_getvar(struct ffunctable *, char *, ftype);
  136. extern struct ffuncstruct *ffunc_findfunc(struct ffunctable *, char *, ftype, int);
  137. extern void ffunc_free(struct ffunctable *);
  138. extern struct ffuncstruct *ffunc_create(struct ffunctable *, char *, fevalfunc, int);
  139. extern void ffunc_free_builtins(void);
  140.  
  141. /* value functions */
  142. extern void fvalue_free(fvalue *);
  143. extern void fvalue_free_parts(fvalue *);
  144. extern void fvalue_from_double(fvalue *,ftype,double);
  145. extern int fvalue_iszero(fvalue *);
  146. extern int fvalue_isone(fvalue *);
  147. #ifdef EOF
  148. extern void fvalue_print(FILE *,fvalue *);
  149. #endif
  150.  
  151. /* miscelaneous other things */
  152. extern fvalue *fderiv_variable;
  153.  
  154. /* default functions and constants */
  155. extern struct ffunctable *fbuiltins;
  156. extern void fbuiltins_build();
  157.  
  158. /* internal stuff */
  159. extern char *fparse_code_internal(struct fcode *, char *);
  160. extern void fparse_set_string(char *);
  161. extern int fparse_yyparse(void);
  162. extern void fparse_yyerror(char *);
  163. extern int fparse_yylex(void);
  164. #ifdef MNEM
  165. extern void fparse_free_dat_int(int, void *, int);
  166. #else
  167. extern void fparse_free_dat(void *, int);
  168. #endif
  169.  
  170. /* external stuff. user defines this, if e wants to change it. default
  171.  * is to just print on stderr. */
  172. extern void fexpr_error(char *);
  173.  
  174. #define FBUF_SIZE 1024
  175.  
  176. #if defined (FCOMPAT)
  177. /* compatibility stuff. don't use this, as it might be removed soon. */
  178.  
  179. struct expression {
  180.   ftype type;
  181.   struct fcode *code;
  182.   struct ffunctable *funcs;
  183. };
  184.  
  185. typedef char *expr_var;    /* just the name */
  186.  
  187. extern struct expression *expr_new(ftype type);
  188. extern void     expr_free(struct expression *);
  189. extern char    *expr_parse(struct expression *expr, char *e);
  190. extern double   expr_evaluate(struct expression *e);
  191. extern void     expr_evaluate_some(struct expression *e, expr_var v,
  192.                    double min, double max, int npoints,
  193.                    double *buffer);
  194. extern void     expr_evaluate_complex(struct expression *e, fcomplex *op);
  195. extern expr_var expr_create_variable(struct expression *e, char *name,
  196.                      double val);
  197. extern void     expr_set_variable(struct expression *e, expr_var v,
  198.                   double val);
  199. extern void     expr_set_variable_complex(struct expression *e, expr_var v,
  200.                       fcomplex *val);
  201. extern double   expr_get_variable(struct expression *e, expr_var v);
  202. extern void     expr_get_variable_complex(struct expression *e, expr_var v,
  203.                       fcomplex *op);
  204. extern void     expr_copy_variables(struct expression *source, struct expression *dest);
  205.  
  206. #endif /* fcompat */
  207.  
  208. #endif /* fexpr_h_included */
  209.